﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using AutoMapper;
using CoreColution.Entity.Basics;
using CoreColution.EntityDto.Basics;
using CoreSolution.IService.Basics;
using CoreSolution.Tools;
using CoreSolution.Tools.WebResult;
using CoreSolution.Vip.Manager;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

namespace CoreSolution.Vip.Controllers
{
    [EnableCors("AllowAllOrigin")]
    [Route("api/Menu")]
    [Produces("application/json")]
    //[ApiController] 不能采用，不然action 就不能以对象形式接受参数
    public class MenuController : ControllerBase
    {
        private readonly IMenuService _IMenuService;
        private readonly IUserRoleService _UserRoleService;
        private readonly IMenuRoleService _IMenuRoleService;
        
        public MenuController(IMenuService menuController, IUserRoleService IUserRoleService, IMenuRoleService IMenuRoleService,
            IMenuService IMenuService)
        {
            _IMenuService = menuController;
            _UserRoleService = IUserRoleService;
            _IMenuRoleService = IMenuRoleService;
        }


        /// <summary>
        /// 获取当前用户的菜单列表(通过权限)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetCurrentMenuList")]
        public async Task<JsonResult> GetCurrentMenuList(string token)
        {
            string x_token =string.IsNullOrEmpty(token)?HttpContext.Request.Headers["X-Token"].ToString():token;
            var userId = (await LoginManager.GetUserIdAsync(x_token)).GetValueOrDefault();

            //获取用户菜单权限
            var RoleIdList = _UserRoleService.Where(t => t.UserId == userId).Select(t =>t.RoleId).ToListObject<Guid>().Distinct().ToList();
            
            var MenuIdList = _IMenuRoleService.Where(t => RoleIdList.Contains(t.RoleId)).Select(t => t.MenuId).ToListObject<Guid>().Distinct().ToList(); ;
            
            var MenuList = _IMenuService.Where(t => MenuIdList.Contains(t.Id)).ToListObject<Menu>();

            return AjaxHelper.JsonResult(HttpStatusCode.OK, "成功", MenuList);
        }




        #region 后台操作
        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetMenuInfoList")]
        public async Task<JsonResult> GetMenuInfoList(MenuSearch model)
        {
            var where = PredicateExtensions.True<Menu>();
            if (!string.IsNullOrEmpty(model.Name))
            {
                where = where.And(x => x.Name.Contains(model.Name));
            }

            if (model.ParentId.HasValue)
            {
                var parentId = model.ParentId.Value;
                where = where.And(x => x.ParentId.Equals(model.ParentId.Value));
            }

            var UserInfo = _IMenuService.Where(where).OrderByDesc(t=>t.CreateDate).Skip((model.PageIndex - 1) * model.PageSize).Take(model.PageSize).ToList();

            return AjaxHelper.JsonResult(HttpStatusCode.OK, "获取成功", UserInfo);
        }

        /// <summary>
        /// 添加编辑用户
        /// </summary>
        /// <param name="model">实体类</param>
        /// <param name="TypeOper">操作:0.添加 1 编辑</param>
        /// <returns></returns>
        [HttpPost]
        [Route("AddEditMenuInfo")]
        public async Task<JsonResult> AddEditMenuInfo(MenuDto model)
        {
           
            var data = Mapper.Map<Menu>(model);
            var Num = 0;
            var Msg = "";

            if (model.TypeOper == 0)
            {
                data.Id = Guid.NewGuid();
                Num = _IMenuService.Add(data);
                Msg = "添加成功";
            }
            else
            {
                Num = _IMenuService.Update(data);
                Msg = "修改成功";
            }
            _IMenuService.SaveChange(Num);
            return AjaxHelper.JsonResult(HttpStatusCode.OK, Msg);
        }

        /// <summary>
        /// 删除菜单信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <param name="TypeOper">操作:0.添加 1 编辑</param>
        /// <returns></returns>
        [HttpPost]
        [Route("DeleteMenuInfo")]
        public async Task<JsonResult> DeleteMenuInfo(Guid Id)
        {
            var num = _IMenuService.Delete(Id);
            _IMenuService.SaveChange(num);
            return AjaxHelper.JsonResult(HttpStatusCode.OK, "删除成功");
        }


        /// <summary>
        /// 菜单信息
        /// </summary>
        /// <param name="Id">Id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetMenuInfo")]
        public async Task<JsonResult> GetMenuInfo(Guid Id)
        {
            var MenuInfo = _IMenuService.FirstOrDefault(t=>t.Id==Id);
            return AjaxHelper.JsonResult(HttpStatusCode.OK, "获取成功",MenuInfo);
        }
        #endregion
    }
}